import Common from '../utils/common'
import { MediaTools } from '../utils/media-type'

export const globalMethods = {
  methods: {
    doClipCopy(text) {
      this.$copyText(text).then(res => {
        this.showMsgToast('复制成功')
      }, function(e) {
        this.showErrorToast('复制失败')
      })
    },
    findInList(list, propkey, targetvalue, propvalue) {
      return Common._findInList(list, propkey, targetvalue, propvalue)
    },
    matchFileType(file) {
      const ret = MediaTools.matchFileType(file)
      // console.log('matchFileType', ret)
      return ret
    },
    isNull(obj) {
      return obj === null || obj === undefined
    },
    notNull(obj) {
      return obj !== null && obj !== undefined
    },
    showMsgToast(defaultMsg) {
      if (defaultMsg === null || defaultMsg === undefined || !(defaultMsg.length > 0)) {
        return
      }
      this.$notify.success({
        title: '成功',
        message: defaultMsg
      })
    },
    showErrorToast(error, defaultMsg) {
      defaultMsg = defaultMsg || '系统异常，请稍后再试'
      console.log('error', error)
      if (error && error.data && error.data.errmsg) {
        this.$notify.error({
          title: '失败',
          message: error.data.errmsg
        })
      } else {
        this.$notify.error({
          title: '失败',
          message: error || defaultMsg
        })
      }
    },
    showErrorConsole(error, defaultMsg) {
      defaultMsg = defaultMsg || '操作异常，请稍后再试'
      console.log(error || defaultMsg)
    },
    JsonParse(str) {
      try {
        return JSON.parse(str)
      } catch (e) {
        return {}
      }
    },
    getPageParam(id, options) {
      const that = this

      const _getFromQuery = function(id) {
        if (that.notNull(that.$route) && that.notNull(that.$route.query)) {
          return that.$route.query[id]
        } else {
          return null
        }
      }

      if (that.notNull(that.$route) && that.notNull(that.$route.params)) {
        return that.$route.params[id] || _getFromQuery(id)
      } else {
        return _getFromQuery(id)
      }
    }
  }
}

